using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace RandomRat {


	/// <summary>
	/// Summary description for CriteriaSummaryControl.
	/// </summary>
	public class CriteriaSummaryControl : System.Windows.Forms.UserControl {
		
		DataManager dataManager;
		DataView view;

		CriteriaFormState _formState = null;
		string _selectedCriteriaID = null;

		Form1 form1 = null;

		public event UserActionHandaler Action;

		#region WINVARS

		private System.Windows.Forms.RichTextBox richTextBox1;
		public System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label24;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem miDelete;
		private System.Windows.Forms.MenuItem miRename;
		private System.ComponentModel.Container components = null;

		#endregion

		public CriteriaSummaryControl()	{
			InitializeComponent();
			contextMenu1.Popup += new EventHandler(contextMenu1_Popup);
		}

		public void LoadData(DataManager dataManager) {
			this.dataManager = dataManager;

			RefreshView();

			validateControls();
		}
		public void SetForm1(Form1 form1) {
			this.form1 = form1;
		}

		public void RefreshView() {			
			// Why is the view going south when a record gets modified?!
			view = new DataView(dataManager.CriteriaDataSet.Tables["Criteria"]);
			view.AllowDelete = true;
			view.AllowEdit = true;
			view.Sort = "CriteriaID ASC";

			listBox1.DataSource = view;
			listBox1.DisplayMember = "CriteriaID";
		}


		private void OnAction(UserActionArgs e) {
			if (Action != null) {
				Action(this, e);
			}
		}


		public string SelectedCritieriaID {
			get { return _selectedCriteriaID; }
		}
		public CriteriaFormState SelectedFormState {
			get { return _formState; }
		}


		private string getSelectedCritieriaID() {
			return (string)((DataRowView)listBox1.SelectedItem).Row["CriteriaID"]; 
		}


		private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
			_selectedCriteriaID = getSelectedCritieriaID();
			_formState = dataManager.GetCriteriaFormState(_selectedCriteriaID);
			richTextBox1.Text = CriteriaFormState.GetSummary(_formState, SummaryType.AbbreviatedNames, false);

			OnAction(new UserActionArgs(UserAction.SelectionChanged, _selectedCriteriaID, _formState));

		}


		private void miRename_Click(object sender, System.EventArgs e) {
			CriteriaName criteriaName = new CriteriaName(dataManager.CriteriaDataSet, _selectedCriteriaID, CriteriaNameDialogType.Rename);
			
			if (criteriaName.ShowDialog(this) == DialogResult.OK) {
				
				int curIndx = listBox1.SelectedIndex;
				
				if (!form1.CurCriteriaHasBeenSaved || form1.CurCriteriaID.CompareTo(_selectedCriteriaID) != 0) {
					dataManager.RenameCriteria(SelectedCritieriaID, criteriaName.CriteriaID);
				} else {
					// Criteria is curently open, rename through from
					form1.RenameCriteria(criteriaName.CriteriaID);
				}
				RefreshView();
				listBox1.SelectedIndex = curIndx;
			}
			criteriaName.Dispose();

		}


		private void deleteSelection() {
			if (view.Table.Rows.Count > 0) {
				if (!form1.CurCriteriaHasBeenSaved || form1.CurCriteriaID.CompareTo(_selectedCriteriaID) != 0) {
					string textMsg = "Are you sure you want to delete '" + SelectedCritieriaID + "'?";
				
					if (MessageBox.Show(this.Parent, textMsg, "", MessageBoxButtons.YesNo) == DialogResult.Yes) {
						view.Delete(listBox1.SelectedIndex);
						validateControls();
					}

				} else {
					MessageBox.Show(this, "This criteria is currently open." + "\n" + "It cannot be deleted.");
				}
			}
		}

		private void selectionMade() {
			if (view.Table.Rows.Count != 0) {
				OnAction(new UserActionArgs(UserAction.SelectionMade, _selectedCriteriaID, _formState));
			}
		}


		private void validateControls() {
			if (view.Table.Rows.Count == 0) {
				richTextBox1.Text = "";
				richTextBox1.Enabled = false;

				_selectedCriteriaID = null;
				_formState = null;

				OnAction(new UserActionArgs(UserAction.DeletedAllCriteria, _selectedCriteriaID, _formState));
			}
		}


		protected override void Dispose( bool disposing ) {
			if( disposing )	{
				if(components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.miDelete = new System.Windows.Forms.MenuItem();
			this.miRename = new System.Windows.Forms.MenuItem();
			this.label1 = new System.Windows.Forms.Label();
			this.label24 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// richTextBox1
			// 
			this.richTextBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.richTextBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.richTextBox1.Location = new System.Drawing.Point(141, 17);
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.ReadOnly = true;
			this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
			this.richTextBox1.Size = new System.Drawing.Size(252, 172);
			this.richTextBox1.TabIndex = 34;
			this.richTextBox1.TabStop = false;
			this.richTextBox1.Text = "Number in set: \t10 (equal types)\n\nElement types:\tA\n\t\tB\n\nSuccession tol: \t3\nEvery-" +
				"other tol: \t4\n\nAdv pattern tol:\t3, 2\n\t\t44, 44";
			this.richTextBox1.WordWrap = false;
			// 
			// listBox1
			// 
			this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left);
			this.listBox1.ContextMenu = this.contextMenu1;
			this.listBox1.DisplayMember = "CriteriaID";
			this.listBox1.Location = new System.Drawing.Point(1, 17);
			this.listBox1.Name = "listBox1";
			this.listBox1.ScrollAlwaysVisible = true;
			this.listBox1.Size = new System.Drawing.Size(136, 173);
			this.listBox1.TabIndex = 32;
			this.listBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listBox1_KeyDown);
			this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
			this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.miDelete,
																						 this.miRename});
			// 
			// miDelete
			// 
			this.miDelete.Index = 0;
			this.miDelete.Text = "Delete";
			this.miDelete.Click += new System.EventHandler(this.miDelete_Click);
			// 
			// miRename
			// 
			this.miRename.Index = 1;
			this.miRename.Text = "Rename";
			this.miRename.Click += new System.EventHandler(this.miRename_Click);
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.Color.Transparent;
			this.label1.Location = new System.Drawing.Point(1, 1);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 33;
			this.label1.Text = "Criteria:";
			// 
			// label24
			// 
			this.label24.Location = new System.Drawing.Point(141, 1);
			this.label24.Name = "label24";
			this.label24.Size = new System.Drawing.Size(100, 16);
			this.label24.TabIndex = 35;
			this.label24.Text = "Summary:";
			// 
			// CriteriaSummaryControl
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.richTextBox1,
																		  this.listBox1,
																		  this.label1,
																		  this.label24});
			this.Name = "CriteriaSummaryControl";
			this.Size = new System.Drawing.Size(396, 192);
			this.ResumeLayout(false);

		}
		#endregion


		private void listBox1_DoubleClick(object sender, System.EventArgs e) {
			selectionMade();
		}

		private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back) {
				deleteSelection();
			} else if (e.KeyCode == Keys.Enter) {
				selectionMade();
			}
		}
		private void miDelete_Click(object sender, System.EventArgs e) {
			deleteSelection();
		}

		private void contextMenu1_Popup(object sender, System.EventArgs e) {
			if (listBox1.SelectedIndex != -1) {
				miDelete.Enabled = true;
				miRename.Enabled = true;
			} else {
				miDelete.Enabled = false;
				miRename.Enabled = false;
			}
		}



	}


    public enum UserAction { SelectionChanged, SelectionMade, DeletedAllCriteria }

    public delegate void UserActionHandaler(object sender, UserActionArgs e);

    /// <summary>
    /// Event argument for a UserAction event
    /// </summary>
    public class UserActionArgs : EventArgs
    {
        public UserAction UserAction;
        public string CriteriaID = null;
        public CriteriaFormState FormState = null;

        // Constructor
        public UserActionArgs(UserAction userAction, string criteriaID, CriteriaFormState formState)
        {
            this.UserAction = userAction;
            this.CriteriaID = criteriaID;
            this.FormState = formState;
        }
    }
}
